package com.example.springbootmybatispage.service;

import com.example.springbootmybatispage.mapper.EmployeeMapper;
import com.example.springbootmybatispage.model.Employee;
import com.example.springbootmybatispage.request.PostByPageParamRequest;
import com.example.springbootmybatispage.request.PostByUsernameLikeRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    public List<Employee> selectPageByRowBounds(int pageIndex, int pageSize) {


        final RowBounds rowBounds = new RowBounds((pageIndex) * pageSize, pageSize);
        List<Employee> employees = this.employeeMapper.selectPageByRowBounds(rowBounds);

        return employees;
    }


    public PageInfo<Employee> selectPageByPageHelper(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Employee> employees = this.employeeMapper.selectPageByPageHelper();
        PageInfo pageInfo = new PageInfo<>(employees);
        System.out.println("分页总数：" + pageInfo.getTotal() + ",分页大小：" + pageInfo.getPages());
        return pageInfo;
    }

    /**
     * @param pageParamRequest
     * @return
     */
    public List<Employee> selectByPageParams(PostByPageParamRequest pageParamRequest) {
        //参数处理，异常处理
        if (pageParamRequest.getPageIndex() == null) {
            pageParamRequest.setPageIndex(1);
        }
        if (pageParamRequest.getPageSize() == null || pageParamRequest.getPageSize() > 10) {
            pageParamRequest.setPageSize(10);
        }
        //1.rowBounds
        RowBounds rowBounds = new RowBounds(pageParamRequest.getPageIndex() * pageParamRequest.getPageSize(),
                pageParamRequest.getPageSize());
        List<Employee> employees = this.employeeMapper.selectPageByRowBounds(rowBounds);

        //2. pageHelper
        PageHelper.startPage(pageParamRequest.getPageIndex(), pageParamRequest.getPageSize());
        List<Employee> employees1 = this.employeeMapper.selectPageByPageHelper();
        return employees;
    }


    public List<Employee> getByUsernameLike(PostByUsernameLikeRequest postByUsernameLikeRequest) {
        if (postByUsernameLikeRequest.getUsername() == null) {
            return new ArrayList<>();
        }
        //1.rowBounds
        RowBounds rowBounds = new RowBounds(0, 10);
        List<Employee> employees = this.employeeMapper.selectByUsernameLike(rowBounds, postByUsernameLikeRequest.getUsername());
        //2. TODO 自己完善第二种方式的查询
        return employees;
    }


    public List<Employee> getByEmailLike(String email) {
        if (email == null) {
            return new ArrayList<>();
        }
        //1.走PageHelper
        PageHelper.startPage(1, 10);
        List<Employee> employees = this.employeeMapper.selectByEmailLike(email);
        // TODO 自己完善RowBounds方式的查询
        return employees;
    }
}
